// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import { join } from 'path';
import { window, commands, workspace, ExtensionContext, ViewColumn, WorkspaceFolder, Uri } from 'vscode';
import { IBizCustomEditor } from './editor/IBizCustomEditor';
import { NodeDependenciesProvider } from './provider/NodeDependenciesProvider';


function getWebContent(context: ExtensionContext): string {
  return `<!DOCTYPE html>
  <html lang="en">
  
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
  </head>
  
  <body>
    <iframe src="http://172.16.170.145/slnstudio/index.html#/common_slnindex/srfkeys=37F7C147-65D7-4717-9F0B-83CEDBA9FE5F/sysdesign_psdevslnsysmodeltreeexpview/srfkey=1E89F943-6B57-45F4-A7C4-9D159BE87738" frameborder="0" style="width: 100vw;height: 100vh;"></iframe>
  </body>
  
  </html>`;
}

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: ExtensionContext) {
  // Use the console to output diagnostic information (console.log) and errors (console.error)
  // This line of code will only be executed once when your extension is activated
  console.log('Congratulations, your extension "Test" is now active!');
  // 注册command命令
  const disposable = commands.registerCommand('Test.IBiz', () => {
    window.showInformationMessage('Hello World from IBiz Test!');
  });
  context.subscriptions.push(disposable);
  // 主分页树
  window.createTreeView('IBizTreeView', { treeDataProvider: new NodeDependenciesProvider(workspace.rootPath!) });
  // explorer 侧边栏 树
  window.createTreeView('IBizTreeView2', { treeDataProvider: new NodeDependenciesProvider(workspace.rootPath!) });
  // 新建面板
  const panel = window.createWebviewPanel('ibiz', 'ibiz panel', ViewColumn.One, { enableScripts: true, retainContextWhenHidden: true, enableCommandUris: true });
  panel.webview.html = getWebContent(context);
  // 新建命令行
  const terminal = window.terminals.find(t => t.name === 'IBizTerminal') || window.createTerminal('IBizTerminal');
  terminal.show();
  terminal.sendText('测试文本内容，网址：https://www.baidu.com');
  // 自定义编辑器
  window.registerCustomEditorProvider('IBizEdit.TestEditor', new IBizCustomEditor());
  run();
}

async function run(): Promise<void> {
  // 读取项目目录下文件内容
  if (workspace.workspaceFolders) {
    const folder: WorkspaceFolder = workspace.workspaceFolders[0];
    const path = folder.uri.path;
    const uri = Uri.file(join(path, '.ibizproject'));
    try {
      const val = await workspace.fs.readFile(uri);
      console.log(val.toString());
    } catch (err) {
      console.error(err);
    }
  }
}

// this method is called when your extension is deactivated
export function deactivate() { }
